package com.modular.extra.fm.site.action;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.modular.extra.fm.FactoryFmOpt;
import com.modular.extra.fm.om.bean.FmOmUserDo;
import com.modular.extra.fm.site.bean.FmSiteInfoDo;
import com.tool.ToolRedisson;
import ntd.action.ActionAbs;
import ntd.action.bo.ActionInfo;
import ntd.action.bo.ActionOutBo;
import ntd.tool.abnormal.ComAbnormal;
import ntd.util.frame.task.handle.fm.util.UtilFmMsg;

import java.time.Duration;

public class FmSendSmsCodeAction extends ActionAbs {

    @Override
    public ActionOutBo handle() {
        String mn = this.actionInBo.getDataCode();

        // 获取基站负责人手机号
        FmSiteInfoDo fmSiteInfoDo = FactoryFmOpt.FM_SITE_OPT.query().eq("mn", mn).getBean();

        String omDeptId = fmSiteInfoDo.getOmDeptId();

        FmOmUserDo fmOmUserDo = FactoryFmOpt.FM_OM_USER_OPT.query().eq("is_leader", "1").eq("om_dept_id", omDeptId).getBean();

        if (ObjectUtil.isNull(fmOmUserDo)) {
            throw new ComAbnormal("找不到部门负责人...");
        }
        String phone = fmOmUserDo.getPhone();

        String code = ToolRedisson.me.get("captcha:" + phone, () -> RandomUtil.randomNumbers(6), Duration.ofSeconds(60 * 5));

        UtilFmMsg.captcha(phone, code);

        return new ActionOutBo();
    }

    @Override
    public ActionInfo info() {
        return new ActionInfo("基站负责人校验码", "fm-site-om-sms-code", this);
    }
}
